//
//  STMPwdModifyViewController.m
//  StoreManagement
//
//  Created by centling on 16/6/1.
//  Copyright © 2016年 alfred. All rights reserved.
//

#import "STMPwdModifyViewController.h"
#import "STMLoginViewController.h"

@interface STMPwdModifyViewController ()
@property (weak, nonatomic) IBOutlet UITextField *oldPwdTextField;
@property (weak, nonatomic) IBOutlet UITextField *modifyPwdTextField;
@property (weak, nonatomic) IBOutlet UITextField *confirmPwdTextField;
@property (weak, nonatomic) IBOutlet UIButton *modifyButton;

@end

@implementation STMPwdModifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = MAINGRAYCOLOR;
    self.modifyButton.backgroundColor = MAINORANGECOLOR;
    [self.modifyButton addTarget:self action:@selector(modifyAction) forControlEvents:UIControlEventTouchUpInside];
}

- (void)modifyAction {
    if ([self.oldPwdTextField.text isEqualToString:@""]) {
        [GlobalUtil HUDShowMessage:@"请输入原密码" addedToView:self.view];
        return;
    }
    if ([self.modifyPwdTextField.text isEqualToString:@""]) {
        [GlobalUtil HUDShowMessage:@"请输入新密码" addedToView:self.view];
        return;
    }
    if ([self.confirmPwdTextField.text isEqualToString:@""]) {
        [GlobalUtil HUDShowMessage:@"请输入确认密码" addedToView:self.view];
        return;
    }
    if (![self.oldPwdTextField.text isEqualToString:[UserDefaultHandler getUserDetaultString:kUserPwd]]) {
        [GlobalUtil HUDShowMessage:@"原密码输入错误" addedToView:self.view];
        return;
    }
    if (![self.modifyPwdTextField.text isEqualToString:self.confirmPwdTextField.text]) {
        [GlobalUtil HUDShowMessage:@"两次密码输入不一致" addedToView:self.view];
        return;
    }
    [STMNetworkAPI modifyPwdWithSellerName:[UserDefaultHandler getUserDetaultString:kSellerName]
                                    newPwd:self.confirmPwdTextField.text
                                   success:^(STMLoginModel *result) {
                                       if ([result.code isEqualToString:@"200"]) {
                                           [UserDefaultHandler saveUserDetault:self.confirmPwdTextField.text key:kUserPwd];
                                           UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
                                           STMLoginViewController *loginVC = [[STMLoginViewController alloc] init];
                                           [nav presentViewController:loginVC animated:YES completion:nil];
                                           [nav popToRootViewControllerAnimated:NO];
                                       } else {
                                           [GlobalUtil HUDShowMessage:result.message addedToView:self.view];
                                       }
                                   }
                                   failure:^(NSError *err) {
                                       [GlobalUtil HUDShowMessage:@"请检查网络连接" addedToView:self.view];
                                   }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
